<?php
/**
 *
 */
namespace app\common\query;
use app\common\model\Order as OrderModel;
use app\common\model\MemberCoin as MemberCoinModel;
use app\common\cache\CacheData;
class Order
{
    static $status = [
                1 => '待发货',
                2 => '待签收',
                3 => '订单完成',
                0 => '订单取消',
            ];

    public static function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }
    /**
     * 获取我的订单
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public static function getRows($where=[],$field='',$queryStr='',$baseUrl=''){
        if(!$baseUrl){
            $baseUrl = url('order.show/index');
        }
        // 每页数量
        $perpagenum = Option::get('perpage_goods_num')?Option::get('perpage_goods_num'):15;
        $rows = self::getDatas($where,$field,$perpagenum);
        $total = $rows['total'];
        $page = $rows['current_page'];
        $rows['page_html'] = pagination($total, $perpagenum, $page, $baseUrl, $queryStr);
        return $rows;
    }


    /**
     * 查询多条数据
     * @return [type] [description]
     */
    public static function getDatas($where=[],$fields='',$perpagenum=null){
        $model = new OrderModel;
        if($perpagenum){
            $data = $model->where($where)->order('create_time desc')->field($fields)->paginate($perpagenum);
            $data = $data->toArray();
            $rows = $data['data'];
            foreach($rows as &$row):
                $row = self::setRemark($row);
            endforeach;
            $data['data'] = $rows;
        }else{

            $data = $model->where($where)->order('create_time desc')->field($fields)->select();

            $data = $data->toArray();
            foreach($data as &$row):
                $row = self::setRemark($row);
            endforeach;
        }
        return $data;
    }

    /**
     * 查询一条订单
     */
    public static function getOneDatas($where=[]){
        $model = new OrderModel;
        $data = $model->where($where)->find();
        $row = self::setRemark($data);
        return $data;
    }

    /**
     * 查询一条订单不做数据处理
     */
    public static function getOneRow($where=[]){
        $model = new OrderModel;
        return $model->where($where)->find();
    }

    /**
     * 查询多条订单不做数据处理
     */
    public static function getAllRow($where=[]){
        $model = new OrderModel;
        return $model->where($where)->field('id,order_id,mid,logis_id,status,credit')->select()->toArray();
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($row){
        if(isset($row['des'])){
            $row['des'] = unserialize($row['des']);
        }
        if(isset($row['create_time'])){
            $row['create_time'] = date("Y-m-d H:i",$row['create_time']);
        }
        if(isset($row['address_id'])){
            $memberAddress = new \app\common\query\MemberAddress;
            $row['userinfo'] =$memberAddress->memberAddressFind($row['address_id']);
        }
        if(isset($row['status'])){
            switch($row['status']){
                case '1':
                   $row['chs_status'] = '待发货';
                    break;
                case '2':
                   $row['chs_status'] = '待签收';
                    break;
                case '3':
                   $row['chs_status'] = '订单完成';
                    break;
                default:
                   $row['chs_status'] = '订单取消';
                    break;
            }
        }
        if(isset($row['order_id'])){
             $row['goods']=\app\common\query\OrderDetails::getDatas(['order_id'=>$row['order_id']]);
             $logis_no = \app\common\query\OrderLogistics::getOneDatas(['logis_order_id'=>$row['order_id']],'logis_no');
             $row['logis_no'] = $logis_no['logis_no'];
        }
        return $row;
    }

    /**
     * [orderMemberCoinRecord 添加资金明细]
     * @param  string $type          [description]
     * @param  string $mid           [description]
     * @param  string $coin_id       [description]
     * @param  string $change_amount [description]
     * @param  string $msg           [description]
     * @return [type]                [description]
     */
    public static function orderMemberCoinRecord($type='',$mid='',$coin_id='',$change_amount='',$msg=''){

        $model = new MemberCoinModel;
        $where = ['mid'=>$mid,'coin_id'=>$coin_id];
        $beforeAmount = $model->where($where)->value('amount');
        $operate = '';
        if($type=='dec'){
            $operate = 'buy';
            $model -> where($where) ->setDec('amount',$change_amount);
        }else{
            $operate = 'rejected';
            $model -> where($where) ->setInc('amount',$change_amount);
        }
        $afterAmount = $model->where($where)->value('amount');
        $record = [
                    'operate_id' => 0,
                    'mid' => $mid,
                    'coin_id' => $coin_id,
                    'amount' => $change_amount,
                    'operate' => $operate,
                    'before_amount' => $beforeAmount,
                    'after_amount' => $afterAmount,
                    'message' => '',
                    'remark' => $msg,
                    'flag' => $type
                ];
        $memberCoinRecordModel = new \app\common\model\MemberCoinRecord;
        $res = $memberCoinRecordModel-> save($record);
        return true;
    }



}